package graph;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

public class GenericGraph {
	private ArrayList<Node> nodes = new ArrayList<Node>();

	public void addNode(String nodeName) {
		nodes.add(new Node(nodeName));
	}

	public ArrayList<Node> getNodes() {
		return nodes;
	}

	public void addEdge(String from, String to, int cost) {
		Node fromNode = getNodeByName(from);
		Node toNode = getNodeByName(to);

		fromNode.addEdge(fromNode, toNode, cost);
		toNode.addIncomingNode(fromNode, toNode, cost);
	}

	public Node getNodeByName(String name) {
		for (Node node : nodes) {
			if (node.getName().equals(name)) {
				return node;
			}
		}
		return null;
	}

	public ArrayList<Node> getPrequelNeighbours(Node node) {
		ArrayList<Node> prequel = new ArrayList<Node>();
		for (Node nd : nodes) {
			for (Node nodeNeighbour : nd.getNextNeighbours()) {
				if (nodeNeighbour.getName().equals(node.getName())) {
					prequel.add(node);
				}
			}
		}
		return prequel;
	}

	public void printAll() {
		for (Node node : nodes) {
			System.out.println(node.toString());
		}
	}
}